#include "menu.h"

int HRES;
int VRES;
int last_sc;
int pontuacao;
string nome;

void header(char * video_mem, char * table) {
	drawHeaderAt("Mastermind", HRES/2-80, 50, 4, 0 , 2 , video_mem, table);
	drawStringAt("**********************", HRES/2-90, 20, 15, 0 , 1 , video_mem, table);
	drawStringAt("**********************", HRES/2-90, 90, 15, 0 , 1 , video_mem, table);
}

void print_ranking(char * video_mem, char * table) {
	cleanscreen(video_mem);
	header(video_mem, table);
	load(nomes, pontos);
	drawStringAt("Ranking", HRES/8, VRES/5, 15, 0 , 2, video_mem, table);
	int yinc= 0;
	do {
		int i;
		for(i = 0; i < 10; i++) {
			drawStringAt(nomes[i], HRES/8, VRES/3+yinc, 15, 0 , 1, video_mem, table);
			if(pontos[i] != 0)
				drawIntAt(pontos[i], HRES/8+200, VRES/3+yinc, 15, 0, 1, video_mem, table);
			yinc += 25;
		}
		drawStringAt("Prima ESC para voltar ao menu anterior", 480, 550, 15, 0 , 1, video_mem, table);
		do
			{
				while(scancode == last_sc);
				last_sc = scancode;
			}while (last_sc != ESC);
		
		if(last_sc == ESC)
		{
			cleanscreen(video_mem);
			return;
		}
	}while(1);
}

void cleanscreen(char * video_mem)
{
	system("cls"); //Limpa o ecra
	clear_screen(0, video_mem);
}

void play(char * video_mem, char * table) {
	pontuacao = 100;
	last_sc = 0;
	cleanscreen(video_mem);
	header(video_mem, table);
	char *word=NULL, *gameword=NULL, *result=NULL, *winword=NULL;
	string guess;
	word = get_random_word();
	winword = win_word(word);
	gameword = hide_word(word);
	int i = 0;
	int n;
	for(n=0; n < 20; n++) {
		guess[n] = ' ';
	}
	do {
		drawStringAt("Palavra escondida", 150, 300, 15, 0 , 1, video_mem, table);
		drawStringAt("Palavra introduzida", 150, 350, 15, 0 , 1, video_mem, table);
		drawStringAt("Chave", 150, 400, 15, 0 , 1, video_mem, table);
		drawStringAt(gameword, 400, 300, 15, 0 , 1, video_mem, table);
		drawStringAt(guess, 400, 350, 15, 0 , 1, video_mem, table);
		drawStringAt("Prima ESC para voltar ao menu anterior", 480, 550, 15, 0 , 1, video_mem, table);
		drawStringAt("Prima ENTER para validar a palavra", 480, 525, 15, 0 , 1, video_mem, table);
		drawStringAt("Prima END para pedir ajuda", 480, 500, 15, 0 , 1, video_mem, table);
		drawStringAt("Prima DEL para introduzir novamente", 480, 475, 15, 0 , 1, video_mem, table);
		drawPinsAt("r", 50, 475, video_mem);
		drawStringAt("- letra no sitio correcto", 60, 470, 15, 0 , 1, video_mem, table);
		drawPinsAt("w", 50, 500, video_mem);
		drawStringAt("- letra no sitio errado", 60, 495, 15, 0 , 1, video_mem, table);
		drawPinsAt("n", 50, 525, video_mem);
		drawStringAt("- letra nao existe na palavra", 60, 520, 15, 0 , 1, video_mem, table);
		
		if(strcmp(gameword, word) == 0)
		{
			if(pontuacao > 0) 
			{
				add_highscore(pontuacao, nome);
			}
			delay(1000);
			break;
		}
		
			do
			{
				while(scancode == last_sc);
				last_sc = scancode;
			}while (last_sc != ESC && last_sc != END && (last_sc < 144 && last_sc > 178) && last_sc != ENTER && last_sc != DEL);
	
		if(last_sc == ESC)
		{
			pontuacao = 0;
			cleanscreen(video_mem);
			return;
		}
		if(last_sc == END)
		{
			if(strcmp(result, winword) == 0)
				continue;
			game_help(word, gameword);
			pontuacao -= 5;
			continue;
		}
		if(last_sc == ENTER)
		{
			guess[i] = '\0';
			if(strlen(gameword) == strlen(guess)) {
				result = validate(word, guess);
				game_guess(word, gameword, guess);
				drawPinsAt(result, 400, 400, video_mem);
			}
			int l;
			for(l=0; l < 15; l++) {guess[l] = ' ';}
			i = 0;
			continue;
		}
		if(last_sc == DEL)
		{
			int k;
			for(k=0; k < 15; k++) {
			guess[k] = ' ';
			}
			i = 0;
			continue;
		}
		else
		{
			char c = getChar(last_sc);
			if(c != ' ') {
				guess[i] = c;
				i++;}
			continue;
		}
	}while(1);
}

void init()
{
	printf("Nome: ");
	fgets(nome, 50, stdin);
	nome[strlen(nome)-1] = '\0';
	delay(500);
	__dpmi_meminfo *map;
	menu escolha=GAME; //predefinir a escolha selecionada como GAME
	_go32_dpmi_seginfo old_kbd_irq, old_t0_irq;
	char * video_mem = NULL;
	video_mem = enter_graphics(0x103, map);
	cleanscreen(video_mem);
	HRES = 800;
	VRES = 600;
	char * table;
	load_char_table(table);
	last_sc = 0;
	install_asm_irq_handler(T0_IRQ,program,&old_t0_irq);
	srand(time(NULL));
	
	
		do
		{
			install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
			header(video_mem, table);
		
		if(escolha==GAME) {
			drawFrame((HRES/8)-12, (VRES/3)-10, (HRES/8)+75, (VRES/3)+25, 4, video_mem);
			drawFrame(((HRES/8)-12)+2, ((VRES/3)-10)+2, ((HRES/8)+75)+2, ((VRES/3)+25)+2, 4, video_mem);
			}
			else {
			drawFrame((HRES/8)-12, (VRES/3)-10, (HRES/8)+75, (VRES/3)+25, 15, video_mem);
			drawFrame(((HRES/8)-12)+2, ((VRES/3)-10)+2, ((HRES/8)+75)+2, ((VRES/3)+25)+2, 15, video_mem);
			}
		drawStringAt("Jogar", HRES/8, (VRES/3), 15, 0 , 1, video_mem, table);
		
		if(escolha==RANKING) {
			drawFrame((HRES/8)-10, (VRES/2)-10, (HRES/8)+75, (VRES/2)+25, 4, video_mem);
			drawFrame(((HRES/8)-10)+2, ((VRES/2)-10)+2, ((HRES/8)+75)+2, ((VRES/2)+25)+2, 4, video_mem);
			}
			else {
			drawFrame((HRES/8)-10, (VRES/2)-10, (HRES/8)+75, (VRES/2)+25, 15, video_mem);
			drawFrame(((HRES/8)-10)+2, ((VRES/2)-10)+2, ((HRES/8)+75)+2, ((VRES/2)+25)+2, 15, video_mem);
			}
		drawStringAt("Ranking", HRES/8, (VRES/2), 15, 0 , 1, video_mem, table);
		
		if(escolha==OPTIONS) {
			drawFrame((HRES/8)-10, (VRES-200)-10, (HRES/8)+75, (VRES-200)+25, 4, video_mem);
			drawFrame(((HRES/8)-10)+2, ((VRES-200)-10)+2, ((HRES/8)+75)+2, ((VRES-200)+25)+2, 4, video_mem);
			}
			else {
			drawFrame((HRES/8)-10, (VRES-200)-10, (HRES/8)+75, (VRES-200)+25, 15, video_mem);
			drawFrame(((HRES/8)-10)+2, ((VRES-200)-10)+2, ((HRES/8)+75)+2, ((VRES-200)+25)+2, 15, video_mem);
			}
		drawStringAt("Opcoes", HRES/8, VRES-200, 15, 0 , 1, video_mem, table);

		if(escolha==EXIT)
		{
			drawFrame((HRES/8)-10, (VRES-100)-10, (HRES/8)+75, VRES-100+25, 4, video_mem);
			drawFrame((HRES/8)-10+2, (VRES-100)-10+2, (HRES/8)+75+2, (VRES-100)+25+2, 4, video_mem);
		}
		else
		{
			drawFrame((HRES/8)-10, (VRES-100)-10, (HRES/8)+75, (VRES-100)+25, 15, video_mem);
			drawFrame((HRES/8)-10+2, (VRES-100)-10+2, (HRES/8)+75+2, (VRES-100)+25+2, 15, video_mem);
		}
		drawStringAt("Sair", (HRES/8)+2, VRES-100, 15, 0 , 1, video_mem, table);
		
			do
			{
				while(scancode == last_sc);
				last_sc = scancode;
			}while (last_sc != UP && last_sc != DOWN && last_sc!=ENTER);
			
			if(last_sc==UP)
			{
				if(escolha==GAME)
					escolha=EXIT;
				else 
					escolha--;
			}
			else if(last_sc==DOWN)
			{
				if(escolha==EXIT)
					escolha=GAME;
				else 
					escolha++;
			}
			else if(last_sc==ENTER) {
				if(escolha==GAME)
				{
					beep();
					play(video_mem, table);
					cleanscreen(video_mem);
					continue;
				}
				if(escolha == RANKING) {
					beep();
					print_ranking(video_mem, table);
					continue;
				}
				if(escolha == OPTIONS) {
					beep();
					continue;
				}
				if(escolha == EXIT) {
					beep();
					reinstall_asm_irq_handler(KBD_IRQ, &old_kbd_irq);
					reinstall_asm_irq_handler(T0_IRQ, &old_t0_irq);
					leave_graphics(*map);
					break;
				}
			}
		}while(1);
}
